package com.bjpowernode.rbac.service.impl;

import com.bjpowernode.rbac.entity.Customer;
import com.bjpowernode.rbac.entity.Department;
import com.bjpowernode.rbac.entity.Employee;
import com.bjpowernode.rbac.mapper.CustomerMapper;
import com.bjpowernode.rbac.query.CustomerQuery;
import com.bjpowernode.rbac.query.EchartsQuery;
import com.bjpowernode.rbac.service.CustomerService;
import com.bjpowernode.rbac.util.DateUtils;
import com.bjpowernode.rbac.util.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/11 9:10
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 客户列表
     * @param customerQuery
     * @return
     */
    @Override
    public PageInfo<Customer> selectCustomerByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectByCondition(customerQuery);
        return new PageInfo<>(list);
    }

    /**
     * 潜在客户列表
     * @param customerQuery
     * @return
     */
    @Override
    public PageInfo<Customer> selectPotentialCustomerByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectPotentialCustomerByCondition(customerQuery);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Customer> selectCustomerPoolByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectCustomerPoolByCondition(customerQuery);
        return new PageInfo<>(list);
    }

    /**
     * 失败客户列表
     * @param customerQuery
     * @return
     */
    @Override
    public PageInfo<Customer> selectFailCustomerByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectFailCustomerByCondition(customerQuery);
        return new PageInfo<>(list);
    }


    /**
     * 正式客户列表
     * @param customerQuery
     * @return
     */
    @Override
    public PageInfo<Customer> selectFormalCustomerByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectFormalCustomerByCondition(customerQuery);
        return new PageInfo<>(list);
    }


    /**
     * 流失客户列表
     * @param customerQuery
     * @return
     */
    @Override
    public PageInfo<Customer> selectLoseCustomerByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectLoseCustomerByCondition(customerQuery);
        return new PageInfo<>(list);
    }



    @Override
    public PageInfo<Customer> selectCustomerStatusByCondition(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectCustomerStatusByCondition(customerQuery);
        return new PageInfo<>(list);
    }

    /**
     * 新增或编辑用户
     * @param customer
     * @param inputTime
     */
    @Override
    public void saveOrUpdate(Customer customer,Long inputTime) {
        if (customer.getId() != null) {
            /**
             * 编辑潜在用户
             */
            Date inputTimeDate = DateUtils.getDate(inputTime);
            customer.setInputTime(inputTimeDate);
            customerMapper.updateByPrimaryKey(customer);
        } else {
            customer.setInputTime(new Date());
            /**
             * 在潜在客户页面添加数据(默认设置状态为潜在客户)
             */
            customer.setStatus(2);
            customer.setSellerId(UserContext.getEmployee().getId());
            customer.setInputUserId(UserContext.getEmployee().getId());
            customerMapper.insert(customer);
        }
    }

    /**
     * 修改客户状态
     * @param customerId
     * @param status
     */
    @Override
    public void updateCustomerStatus(Long customerId, Integer status) {
        customerMapper.updateCustomerStatus(customerId,status);
    }

    /**
     * 移交之后对于customer表的修改
     * @param customerId
     * @param newSellerId
     */
    @Override
    public void editSellerId(Long customerId, Long newSellerId) {
        customerMapper.editSellerId(customerId,newSellerId);
    }

    @Override
    public PageInfo<EchartsQuery> getEchartsQueryList(CustomerQuery customerQuery,String keyword,String dateBegin,String dateEnd) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        Map<String, Object> map = countCustomerMake(customerQuery.getGroupType(),keyword,dateBegin,dateEnd);
        List<EchartsQuery> list = new ArrayList<>();
        ArrayList<Object> names = (ArrayList<Object>) map.get("data1");
        String[] name = new String[names.size()];
        ArrayList<Object> nums = (ArrayList<Object>) map.get("data2");
        Integer[] num = new Integer[nums.size()];
        Iterator<Object> itNames = names.iterator();
        Iterator<Object> itNums = nums.iterator();
        int i = 0;
        int j = 0;
        while (itNames.hasNext()) {
            String nameFor = (String) itNames.next();
            name[i++] = nameFor;
        }
        while (itNums.hasNext()) {
            Integer numFor = (Integer) itNums.next();
            num[j++] = numFor;
        }

        for (int k = 0; k < name.length; k++) {
            EchartsQuery query = new EchartsQuery();
            query.setName(name[k]);
            query.setNum(num[k]);
            list.add(query);
        }
        return new PageInfo<>(list);
    }




    /**
     * 查询潜在客户报表
     *
     * @return
     */
    @Override
    public Map<String, Object> countCustomerMake(Integer groupType,String keyword,String dateBegin,String dateEnd) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        //查询客户构成数据的列表
        switch (groupType) {
            case 0:
                dataList = customerMapper.countCustomerMakeByEmp(keyword,dateBegin,dateEnd);
                break;
            case 1:
                dataList = customerMapper.countCustomerMakeByYear(keyword,dateBegin,dateEnd);
                break;
            case 2:
                dataList = customerMapper.countCustomerMakeByMonth(keyword,dateBegin,dateEnd);
                break;
            case 3:
                dataList = customerMapper.countCustomerMakeByDay(keyword,dateBegin,dateEnd);
                break;
            default:
                System.out.println("未查到对应类型的数据及图表信息！");
        }

        //柱状图X轴的数据(目前暂为员工名) 数组
        List<String> data1 = new ArrayList<>();
        //柱状图Y轴的数据(新增客户数)  数组
        List<Integer> data2 = new ArrayList<>();

        //判断数据列表，循环设置数据
        if (dataList != null && dataList.size() > 0) {
            //遍历集合
            dataList.forEach(m -> {
                //获取name对应的数据，设置到X轴的集合中
                data1.add(m.get("groupType").toString());
                //获取total对应的数据，设置到Y轴的集合中
                data2.add(Integer.parseInt(m.get("total").toString()));
            });
        }
        //将X轴的数据和Y轴的数据添加到map集合中
        map.put("data1", data1);
        map.put("data2", data2);
        return map;
    }


    /**
     * 饼状图
     *
     * @return
     */
    @Override
    public Map<String, Object> countCustomerMake02(Integer groupType,String keyword,String dateBegin,String dateEnd) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        //查询客户构成数据的列表
        switch (groupType) {
            case 0:
                dataList = customerMapper.countCustomerMakeByEmp(keyword,dateBegin,dateEnd);
                break;
            case 1:
                dataList = customerMapper.countCustomerMakeByYear(keyword,dateBegin,dateEnd);
                break;
            case 2:
                dataList = customerMapper.countCustomerMakeByMonth(keyword,dateBegin,dateEnd);
                break;
            case 3:
                dataList = customerMapper.countCustomerMakeByDay(keyword,dateBegin,dateEnd);
                break;
            default:
                System.out.println("未查到对应类型的数据及图表信息！");
        }
        //饼状图的数据 数组(数组中是字符串)
        List<String> data1 = new ArrayList<>();
        //饼状图的数据 数组(数组中是对象)
        List<Map<String, Object>> data2 = new ArrayList<>();

        //判断数据列表，循环设置数据
        if (dataList != null && dataList.size() > 0) {
            //遍历集合
            dataList.forEach(m -> {
                //饼状图数据 数组(数组中是字符串)
                data1.add(m.get("groupType").toString());

                //饼状图数据 数组(数组中是对象)
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("name", m.get("groupType"));
                dataMap.put("value", m.get("total"));
                data2.add(dataMap);
            });
        }
        //将X轴的数据和Y轴的数据添加到map集合中
        map.put("data1", data1);
        map.put("data2", data2);
        return map;
    }


}
